package com.empire.module.member.service.event;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.event.vo.*;
import com.empire.module.member.dal.dataobject.event.EventDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.member.dal.mysql.event.EventMapper;

import javax.annotation.Resource;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.*;

/**
 * 境外人员事件管理 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class EventServiceImpl implements EventService {

    @Resource
    private EventMapper eventMapper;

    @Override
    public String createEvent(EventSaveReqVO createReqVO) {
        // 插入
        EventDO event = BeanUtils.toBean(createReqVO, EventDO.class);
        eventMapper.insert(event);
        // 返回
        return event.getId();
    }

    @Override
    public void updateEvent(EventSaveReqVO updateReqVO) {
        // 校验存在
        validateEventExists(updateReqVO.getId());
        // 更新
        EventDO updateObj = BeanUtils.toBean(updateReqVO, EventDO.class);
        eventMapper.updateById(updateObj);
    }

    @Override
    public void deleteEvent(String id) {
        // 校验存在
        validateEventExists(id);
        // 删除
        eventMapper.deleteById(id);
    }

    private void validateEventExists(String id) {
        if (eventMapper.selectById(id) == null) {
            throw exception(EVENT_NOT_EXISTS);
        }
    }

    @Override
    public EventDO getEvent(String id) {
        return eventMapper.selectById(id);
    }

    @Override
    public PageResult<EventDO> getEventPage(EventPageReqVO pageReqVO) {
        return eventMapper.selectPage(pageReqVO);
    }

}